// pages/common/system/index.js
import {getSystemList} from "../../../service/Message";
import {getStorageSync} from "../../../utils/Storage";
import Config from "../../../main.config";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        //招聘信息
        system: {
            loading: false,
            reset: false,
            error: false,
            total: -1,
            list: []
        },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.data._registerId = getStorageSync(Config.storage.registerInfo + ".id");
        this.setData({"system.loading": true});
    },

    /*
    * 列表加载
    * */
    onLoadList: async function (e) {
        try {
            let paging = e.detail.paging;
            let list = this.data.system.list;
            let isReset = this.data.system.reset;
            let registerId = this.data._registerId;
            let res = await getSystemList({
                messageUserid: registerId,
                page: paging.page,
                limit: paging.pageSize
            });
            let newList = res.data.map(function (res) {
                return {
                    createTime: res.createTime.split(";")[0],
                    message: res.message ? JSON.parse(res.message) : {},
                    status: res.status
                }
            });
            list = isReset ? newList : list.concat(newList);
            this.setData({
                "system.list": list,
                "system.total": res.count
            });
        } catch (e) {
            console.log(e)
            this.setData({"system.error": true})
        } finally {
            this.setData({
                "system.reset": false,
                "system.loading": false
            })
        }
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.setData({"system.loading": true})
    }
})